class Solution {
    int printIndex = 0;
    public TreeNode buildTreeChild(int[] preorder, int[] inorder,int beginIndex, int endIndex){
        if(beginIndex > endIndex){
            return null;
        }
        int print = preorder[printIndex];
        TreeNode root = new TreeNode(print);
        int Index = findIndex(inorder,print,beginIndex,endIndex);

        printIndex++;
        root.left = buildTreeChild(preorder,inorder,beginIndex,Index-1);
        root.right =buildTreeChild(preorder,inorder,Index+1,endIndex) ;

        return root;
    }
    public int  findIndex(int[] inorder, int print, int beginIndex, int endIndex){
        for (int i = beginIndex; i <= endIndex; i++) {
            if(print == inorder[i]){
                return i;
            }
        }
        return -1;
    }
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if(preorder == null || inorder == null){
            return null;
        }
        return buildTreeChild(preorder, inorder, 0, inorder.length-1);
    }
}